import request from '@/utils/request';
import type { Member, MemberListData, MemberQuery, BalanceLog, PointsLog, MemberCard, MemberLevel } from '@/types/member';

/**
 * 获取会员列表
 * @param params
 * @returns
 */
export const fetchMembers = (params: MemberQuery) => {
  return request<MemberListData>({
    url: '/v1/member',
    method: 'GET',
    data: params,
  });
};

/**
 * 获取会员详情
 * @param id 会员ID
 * @returns
 */
export const fetchMemberDetail = (id: number) => {
  return request<Member>({
    url: `/v1/member/${id}`,
    method: 'GET',
  });
};

/**
 * 创建会员
 * @param data 会员信息
 * @returns
 */
export const createMember = (data: Partial<Member>) => {
  return request<Member>({
    url: '/v1/member',
    method: 'POST',
    data,
  });
};

/**
 * 更新会员信息
 * @param id 会员ID
 * @param data 会员信息
 * @returns
 */
export const updateMember = (id: number, data: Partial<Member>) => {
  return request<Member>({
    url: `/v1/member/${id}`,
    method: 'PUT',
    data,
  });
};

/**
 * 调整会员余额
 * @param id 会员ID
 * @param amount 调整金额(分)
 * @param remark 备注
 * @returns
 */
export const adjustMemberBalance = (id: number, amount: number, remark: string) => {
  return request({
    url: `/v1/member/${id}/balance`,
    method: 'POST',
    data: { amount, remark },
  });
};

/**
 * 调整会员积分
 * @param id 会员ID
 * @param points 调整积分
 * @param remark 备注
 * @returns
 */
export const adjustMemberPoints = (id: number, points: number, remark: string) => {
  return request({
    url: `/v1/member/${id}/points`,
    method: 'POST',
    data: { points, remark },
  });
};

/**
 * 获取会员余额记录
 * @param memberId 会员ID
 * @param params 查询参数
 * @returns
 */
export const fetchMemberBalanceLog = (memberId: number, params?: any) => {
  return request<{ list: BalanceLog[]; total: number }>({
    url: `/v1/member/${memberId}/balance/log`,
    method: 'GET',
    data: params,
  });
};

/**
 * 获取会员积分记录
 * @param memberId 会员ID
 * @param params 查询参数
 * @returns
 */
export const fetchMemberPointsLog = (memberId: number, params?: any) => {
  return request<{ list: PointsLog[]; total: number }>({
    url: `/v1/member/${memberId}/points/log`,
    method: 'GET',
    data: params,
  });
};

/**
 * 获取会员卡列表
 * @param memberId 会员ID
 * @returns
 */
export const fetchMemberCards = (memberId: number) => {
  return request<{ list: MemberCard[] }>({
    url: `/v1/member/${memberId}/cards`,
    method: 'GET',
  });
};

/**
 * 创建会员卡
 * @param data 会员卡信息
 * @returns
 */
export const createMemberCard = (data: Partial<MemberCard>) => {
  return request<MemberCard>({
    url: '/v1/member/card',
    method: 'POST',
    data,
  });
};

/**
 * 更新会员卡
 * @param id 会员卡ID
 * @param data 会员卡信息
 * @returns
 */
export const updateMemberCard = (id: number, data: Partial<MemberCard>) => {
  return request<MemberCard>({
    url: `/v1/member/card/${id}`,
    method: 'PUT',
    data,
  });
};

/**
 * 获取会员等级列表
 * @returns
 */
export const fetchMemberLevels = () => {
  return request<{ list: MemberLevel[] }>({
    url: '/v1/member/level',
    method: 'GET',
  });
};